package com.ruoyi.food.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.excel.ExcelUtils;
import com.ruoyi.food.domain.SysFood;
import com.ruoyi.food.service.ISysFoodService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 待会吃什么Controller
 *
 * @author 刘哲贤
 * @date 2024-03-25
 */
@RestController
@RequestMapping("/food/whatToEat")
public class SysFoodController extends BaseController {
    @Resource
    private ISysFoodService sysFoodService;

    /**
     * 查询待会吃什么列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysFood sysFood) {
        startPage();
        List<SysFood> list = sysFoodService.selectSysFoodList(sysFood);
        return getDataTable(list);
    }

    /**
     * 前端查询全部数据并返回
     *
     * @param sysFood
     * @return
     */
    @GetMapping("/autoComplete")
    public AjaxResult autoComplete(SysFood sysFood) {
        // 查询没条件就不查 直接返回空
        if (sysFood == null || StringUtils.isEmpty(sysFood.getFoodName())) {
            return AjaxResult.success();
        }
        return AjaxResult.success(sysFoodService.selectSysFoodList(sysFood));
    }

    /**
     * 前端查询全部数据并返回
     *
     * @param sysFood
     * @return
     */
    @GetMapping("/totalList")
    public AjaxResult totalList(SysFood sysFood) {
        return AjaxResult.success(sysFoodService.selectSysFoodList(sysFood));
    }

    /**
     * 导出待会吃什么列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysFood sysFood) {
        List<SysFood> list = sysFoodService.selectSysFoodList(sysFood);
        ExcelUtils<SysFood> util = new ExcelUtils<SysFood>(SysFood.class);
        util.exportData(response, list, "待会吃什么数据");
    }

    /**
     * 获取待会吃什么详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysFoodService.selectSysFoodById(id));
    }

    /**
     * 新增待会吃什么
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SysFood sysFood) {
        return toAjax(sysFoodService.insertSysFood(sysFood));
    }

    /**
     * 修改待会吃什么
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SysFood sysFood) {
        return toAjax(sysFoodService.updateSysFood(sysFood));
    }

    /**
     * 删除待会吃什么
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysFoodService.deleteSysFoodByIds(ids));
    }
}
